package com.marathon.ershor.textrecognizer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONObject;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by ershor on 2017/8/14.
 */

public class RecResultView extends View {

    private Paint mPaint;
    private Point mCenterPoint;
    private CopyOnWriteArrayList<RecObject> mArrayObject;

    public class RecObject {
        public String content;
        public String label;
        public Rect rect;
    }

    public void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mArrayObject = new CopyOnWriteArrayList<>();
    }

    public RecResultView(Context context) {
        super(context);
        init();
    }

    public RecResultView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RecResultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public RecResultView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public void setJson(String json) {
        // parse json, show
        json = "[{\"content\": \"e\", \"topleft\": {\"y\": 96, \"x\": 23}, \"bottomright\": {\"y\": 160, \"x\": 135}, \"label\": \"\\u6587\\u5b57\"}]";
        try {
            mArrayObject.clear();
            JSONArray root = new JSONArray(json);
            for (int i = 0; i < root.length(); i++) {
                JSONObject one = (JSONObject) root.get(i);
                RecObject obj = new RecObject();
                obj.content = one.get("content").toString();
                obj.label = one.get("label").toString();
                JSONObject topleft = (JSONObject) one.get("topleft");
                JSONObject bottomright = (JSONObject) one.get("bottomright");
                obj.rect = new Rect();
                obj.rect.left = Integer.parseInt(topleft.get("x").toString());
                obj.rect.top = Integer.parseInt(topleft.get("y").toString());
                obj.rect.right = Integer.parseInt(bottomright.get("x").toString());
                obj.rect.bottom = Integer.parseInt(bottomright.get("y").toString());
                mArrayObject.add(obj);
            }
            randomObject();
            invalidate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void randomObject() {
//        int iHeight = height / 5;
//        int iWidth = width / 5;
//        if (mArrayObject.isEmpty()) {
//            RecObject obj = new RecObject();
//            obj.content = "物体1";
//            obj.label = "人";
//            obj.rect = new Rect(iWidth, iHeight, iWidth+iWidth,iHeight+iHeight);
//            mArrayObject.add(obj);
//
//            RecObject obj1 = new RecObject();
//            obj1.content = "物体2";
//            obj1.label = "花";
//            obj1.rect = new Rect(3*iWidth, iHeight, 3*iWidth+iWidth,iHeight+iHeight);
//            mArrayObject.add(obj1);
//
//            RecObject obj2 = new RecObject();
//            obj2.content = "物体3";
//            obj2.label = "马";
//            obj2.rect = new Rect(iWidth, 3*iHeight, iWidth+iWidth,3*iHeight+iHeight);
//            mArrayObject.add(obj2);
//        }
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘空心矩形
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        for (int i=0; i<mArrayObject.size(); i++) {
            canvas.drawRect(mArrayObject.get(i).rect, mPaint);
        }

        // 中心点
        int width = this.getWidth();
        int height = this.getHeight();
        mCenterPoint = new Point(width/2, height/2);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Paint.Style.STROKE);
        int crossLineLength = mCenterPoint.x / 5;
        canvas.drawLine(mCenterPoint.x, mCenterPoint.y-crossLineLength,
                mCenterPoint.x, mCenterPoint.y+crossLineLength, mPaint);
        canvas.drawLine(mCenterPoint.x-crossLineLength, mCenterPoint.y,
                mCenterPoint.x+crossLineLength, mCenterPoint.y, mPaint);

        // 绘文字
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeWidth(2);
        mPaint.setTextSize(50);
        canvas.drawText("Scanning...", 5, 100, mPaint);

        // 绘图
        // 从资源文件中生成位图
        //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);
        // 绘图
        //canvas.drawBitmap(bitmap, 10, 60, mPaint);
    }
}
